package com.jasic.comnt.protocol.bean;

import com.jasic.common.GlobalConstant;
import com.jasic.common.exception.HandleException;
import com.jasic.comnt.protocol.MessageCommon;
import com.jasic.util.ByteUtil;

/**
 * User: Jasic
 * Date: 12-12-25
 *
 * Request for getting postion
 */
public class PostionReq extends BaseRequest {

    // 8byte BCD
    private String target_ID;

    //0：LBS；1：GPS；2：北斗；其他待定
    private int target_type;

    public PostionReq(String target_ID, int target_type) {
        super(new MessageCommon(GlobalConstant.CMD_POSTION, GlobalConstant.POSTION_LENGTH));

        this.target_ID = target_ID;
        this.target_type = target_type;

        super.initCheckNum();
    }

    public PostionReq(byte[] data)throws HandleException {
        super(data);
    }

    @Override
    protected byte[] getMsgByte() {
        byte[] bytes = new byte[8 + 1];
        int id_len = this.target_ID.length();

        // 高位补0
        StringBuilder sb = new StringBuilder();
        // 当字符串 ‘12’ -->转成bcd，0x12时长度减半
        for (int i = 0; i < GlobalConstant.POSTION_TARGET_ID_LENGTH * 2 - id_len; i++) {
            sb.append("0");
        }
//        logger.info("--------------userName HEX:");
        byte[] bcd_target_id = ByteUtil.stringToBCD(sb.toString() + this.getTarget_ID());
        System.arraycopy(bcd_target_id, 0, bytes, 0, bcd_target_id.length);
        bytes[bytes.length - 1] = (byte)target_type;
        return bytes;
    }

    @Override
    protected void setValues() {

        this.target_ID = ByteUtil.bcdToString(super.msgByte,0,8);
        this.target_type = super.msgByte[8];
    }

    public String getTarget_ID() {
        return target_ID;
    }

    public void setTarget_ID(String target_ID) {
        this.target_ID = target_ID;
    }

    public int getTarget_type() {
        return target_type;
    }

    public void setTarget_type(int target_type) {
        this.target_type = target_type;
    }
}
